package bm.com.ai.server.vector.config;

import dev.langchain4j.store.embedding.qdrant.QdrantEmbeddingStore;
import io.qdrant.client.QdrantClient;
import io.qdrant.client.QdrantGrpcClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.time.Duration;

@Data
@Component
@ConfigurationProperties(prefix = "qdrant")
public class QdrantConfig {

    private String host;
    private int port;
    private int grpcPort;
    private Integer dimension;
    private CollectionConfig collection = new CollectionConfig();
    private Duration timeoutMs =Duration.ofHours(12);

    @Data
    public static class CollectionConfig {
        private String name;
        private int vectorSize;
        private String distance;
    }

    @Bean
    public QdrantClient getClient() {
        return new QdrantClient(
                QdrantGrpcClient.newBuilder(host,grpcPort,false)
                        .withTimeout(timeoutMs)
                        .build());
    }

    @Bean("qdrantEmbeddingStore")
    public QdrantEmbeddingStore getStore(){
        return QdrantEmbeddingStore.builder()
                .host( host)
                .port(grpcPort)
                .client(this.getClient())
                .collectionName(collection.getName())
                .build();
    }
}